package study.javaee.practice.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import study.javaee.practice.dao.ProjectJoinApplyDao;
import study.javaee.practice.dao.ProjectJoinApplyStateDao;
import study.javaee.practice.entity.ProjectJoinApply;
import study.javaee.practice.entity.User;
import study.javaee.practice.entity.vo.ProjectJoinApplyVO;
import study.javaee.practice.service.ProjectJoinApplyService;
import study.javaee.practice.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectJoinApplyServiceImpl implements ProjectJoinApplyService {
    @Autowired
    private ProjectJoinApplyDao projectJoinApplyDao;
    @Autowired
    UserService userService;

    @Override
    public List<ProjectJoinApplyVO> findAllProjectJoinApplyVO(ProjectJoinApplyVO data) {
        User currentUser = userService.getCurrentUser();
        String name= data.getName();
        String username= data.getUsername();
        Integer state=data.getState();
        if(name==""&&username==""&&state==null) {
            if(currentUser.getType()==2 || currentUser.getType()==3){
                List<ProjectJoinApplyVO> projectJoinApplyVOS = projectJoinApplyDao.getAllProjectJoinApplyVo2();
                return projectJoinApplyVOS;
            }
            else{
                List<ProjectJoinApplyVO> projectJoinApplyVOS = projectJoinApplyDao.getAllProjectJoinApplyVo(currentUser.getId());
                return projectJoinApplyVOS;
            }

        }
        else {
            if(name!=""){
                String name1="%"+name+"%";
                List<ProjectJoinApplyVO> projectJoinApplyVOS = projectJoinApplyDao.getProjectJoinApplyVoByName(name1,currentUser.getId());
                return projectJoinApplyVOS;
            }
            else if(username!=""){
                String username1="%"+username+"%";
                List<ProjectJoinApplyVO> projectJoinApplyVOS = projectJoinApplyDao.getProjectJoinApplyVoByUsername(username1,currentUser.getId());
                return projectJoinApplyVOS;
            }
            else if(state!=null){
                List<ProjectJoinApplyVO> projectJoinApplyVOS = projectJoinApplyDao.getProjectJoinApplyVoByState(state,currentUser.getId());
                return projectJoinApplyVOS;
            }
            else {
                return null;
            }
        }
    }


    @Autowired
    private ProjectJoinApplyStateDao projectJoinApplyStateDao;
    @Override
    public Boolean updateProjectJoinApplyState(ProjectJoinApplyVO data) {
        ProjectJoinApply projectJoinApply=new ProjectJoinApply();

        int project_Id= data.getId();
        int user_Id= data.getUserId();
        int State=data.getState();

        projectJoinApply.setState(State);
        UpdateWrapper<ProjectJoinApply> wrapper = new UpdateWrapper<ProjectJoinApply>();
        Map<String , Object> map = new HashMap<>();
        map.put("project_id" , project_Id);
        map.put("user_id" , user_Id);
        wrapper.allEq(map);
        projectJoinApplyStateDao.update(projectJoinApply,wrapper);
        return true;
    }
}
